package gang.org.springframework.framework.annotation;

import gang.org.springframework.framework.core.annotation.GangAnnotationFilter;

import java.util.Arrays;

public class GangPackagesAnnotationFilter implements GangAnnotationFilter {

    private final String[] prefixes;

    private final int hashCode;

    public GangPackagesAnnotationFilter(String... packages){
        this.prefixes = new String[packages.length];
        for (int i = 0; i < packages.length; i++) {
            String pkg = packages[i];
            this.prefixes[i] = pkg + ".";
        }
        Arrays.sort(this.prefixes);
        this.hashCode = Arrays.hashCode(this.prefixes);
    }

    /**
     * prefixed[] = {"java.lang","org.springframework.lang"}
     * @param annotationType value = "gang.org.springframework.boot.autoconfigure.GSpringBootApplication"
     * */
    @Override
    public boolean matches(String annotationType) {
        for (String prefix : this.prefixes) {
            if (annotationType.startsWith(prefix)) {
                return true;
            }
        }
        return false;
    }
}
